﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;

public partial class MainPage : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Request.Cookies["StudentName"] != null && Request.Cookies["Password"] != null)
            {
                LOGUSERTextBox1.Text = Request.Cookies["StudentName"].Value;
                LOGPASSTextBox2.Attributes["value"] = Request.Cookies["Password"].Value;
            }
        }
    }
    protected void LOGButton1_Click(object sender, EventArgs e)
    {

    }
    protected void LoginButton1_Click(object sender, EventArgs e)

    {    

        if (CheckBox1.Checked)
        {
            Response.Cookies["StudentName"].Expires = DateTime.Now.AddDays(30);
            Response.Cookies["Password"].Expires = DateTime.Now.AddDays(30);
        }
        else
        {
            Response.Cookies["StudentName"].Expires = DateTime.Now.AddDays(-1);
            Response.Cookies["Password"].Expires = DateTime.Now.AddDays(-1);

        }
        Response.Cookies["StudentName"].Value = LOGUSERTextBox1.Text.Trim();
        Response.Cookies["Password"].Value = LOGPASSTextBox2.Text.Trim();



        string username = "admin";
        string PPassword = "admin";

        if (LOGUSERTextBox1.Text == username && LOGPASSTextBox2.Text == PPassword)
        {
            Response.Redirect("~/Members2.aspx");
        }
        else
        {


            SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["RegisterationDBConnectionString"].ConnectionString);
            conn.Open();
            string checkuser = "select count (*) from Student_Information where StudentName='" + LOGUSERTextBox1.Text + "' ";
            SqlCommand com = new SqlCommand(checkuser, conn);
            int temp = Convert.ToInt32(com.ExecuteScalar().ToString());
            conn.Close();
            if (temp == 1)
            {
                conn.Open();
                string checkPWQuery = "select Password from Student_Information where StudentName='" + LOGUSERTextBox1.Text + "' ";
                SqlCommand passcom = new SqlCommand(checkPWQuery, conn);
                string password = passcom.ExecuteScalar().ToString().Replace(" ", "");
                if (password == LOGPASSTextBox2.Text)
                {
                    Session["NEW"] = LOGUSERTextBox1.Text;
                    Response.Write("Password Is Correct");
                    Response.Redirect("Post-Registeration.aspx");
                }
                else
                {
                    LoginLabel1.Text = "Invalid user name or Password";

                }
            }
            else
            {
                LoginLabel1.Text = "Invalid";

            }
            
        }
        
    }


        //else
        //{
        //    using (RegisterationDBEntities DB = new RegisterationDBEntities())
        //    {
        //        Student_Information Student = new Student_Information();
        //        Student.Password = LOGUSERTextBox1.Text;
        //        Student.StudentName = LOGPASSTextBox2.Text;
        //        if (Student.Password == Student.StudentName)
        //        {
        //            if (LOGUSERTextBox1.Text == "Admin")
        //            {
        //                Session["StudentName"] = LOGUSERTextBox1.Text;
        //                Response.Redirect("~/Members2.aspx");
        //            }
        //            else
        //            {
        //                Session["StudentName"] = LOGUSERTextBox1.Text;
        //                Response.Redirect("~/Post-Registeration.aspx");

        //            }

        //        }
        //        else

        //            LoginLabel1.Text = "Invalid User name Or Password";


        //    }
        //}

    //}
    protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
    {

    }
}